package m.jclecai.com.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.base.recyclerview.BaseRecyclerViewAdapter;
import m.jclecai.com.base.recyclerview.BaseViewHolder;
import m.jclecai.com.bean.Response.ResLotteryEncyclopediasItems;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by Dinosa on 2018/1/3.
 * 彩票百科的界面
 */

public class LotteryEncyclopediasActivity extends BaseActivity {

    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    private BaiKeLottery mBaiKeLotteryAdapter;

    @Override
    protected int getContentView() {
        return R.layout.activity_lottery_encyclopedias;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("彩票百科");
        leftOptionText("");
    }


    @Override
    protected void initData() {
        super.initData();

        mBaiKeLotteryAdapter = new BaiKeLottery(null);
        mRvList.setAdapter(mBaiKeLotteryAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        getData();
    }

    public void getData() {

        HttpManager.getAsyn(Constant_Url.LOTTERY_BAIKE, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("网络异常！！！");
            }

            @Override
            public void onResponse(String response) {
                try {
                    if (!TextUtils.isEmpty(response)) {

                        ResLotteryEncyclopediasItems resLotteryEncyclopediasItems = FastOk.get(response, ResLotteryEncyclopediasItems.class);
                        mBaiKeLotteryAdapter.refresh(resLotteryEncyclopediasItems.getRecords());

                    }else{
                        DisPlay("服务器没有数据");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    DisPlay("网络异常！");
                }
            }
        });
    }

    public class BaiKeLottery extends BaseRecyclerViewAdapter<ResLotteryEncyclopediasItems.LotteryEncyclopediasItem>{

        public BaiKeLottery(List<ResLotteryEncyclopediasItems.LotteryEncyclopediasItem> DList) {
            super(DList);
        }

        @Override
        public int getItemView() {
            return R.layout.item_lottery_encyclopedias_list;
        }

        @Override
        protected void bindViewHolder(BaseViewHolder holder, final ResLotteryEncyclopediasItems.LotteryEncyclopediasItem bean, int position) throws ParseException {

            holder.getTextView(R.id.tv_desc).setText(bean.getName());
            Glide.with(LotteryEncyclopediasActivity.this).load(bean.getImage()).into(holder.getImageView(R.id.iv_img));

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //这里进行一个跳转；
                    startActivity( NewWebViewHelperActivity.getIntent(LotteryEncyclopediasActivity.this,bean.getUrl(),"资讯详情"));
                }
            });
        }
    }
}
